Guided Practice 8.3

Modify 08-2-binary-search.rkt to design the following function:

;; binary-search-count : Nat (Nat -> Real) -> Nat
;; GIVEN: a natural number N, and a function f : Nat -> Real
;; WHERE: f is monotonic (ie, i<=j implies f(i) <= f(j))
;; RETURNS: the number of i in [0,N] s.t. f(i) = f(N)

;; EXAMPLES/TESTS:

; list-to-fn : ListOfReal -> (Nat -> Real)
(define (list-to-fn lst)
  (lambda (n)
    (list-ref lst n)))

(define fn1 (list-to-fn '(3 4 5 5 5 6 6 6 7 7 7 8)))

(begin-for-test
  (check-equal?
    (binary-search-count 11 fn1)
    1)
  (check-equal?
    (binary-search-count 10 fn1)
    3)
  (check-equal?
    (binary-search-count 9 fn1)
    2)
  (check-equal?
    (binary-search-count 2 fn1)
    1)
  (check-equal?
    (binary-search-count 3 fn1)
    2)
  (check-equal?
   (binary-search-count 4 fn1)
   3)
  )

Remember that your invariants,halting measure, and termination argument are at least as important as your code.

[ANSWER]


Last modified: Thu Nov 5 13:43:20 Eastern Standard Time 2015